// pages/operation/clueManage/openClassListen/index.js
import { ClassModel } from "../../../../api/api-work/classModel.js";
import Toast from "@vant/weapp/toast/toast";
import { timestampToTime } from "../../../../utils/index.js";
import fromRules from "../../../../utils/rules-form.js";
import formRules from "../../../../utils/rules-form.js";
const classModel = new ClassModel();

const rules = {
  classId: [
    {
      required: true,
      message: "请选择班级",
    },
  ],
  courseId: [
    {
      required: true,
      message: "请选择课程",
    },
  ],
  classDates: [
    {
      required: true,
      message: "请选择上课日期",
    },
  ],
  classStartTime: [
    {
      required: true,
      message: "请选择上课时间",
    },
  ],
  classEndTime: [
    {
      required: true,
      message: "请选择下课时间",
    },
  ],
  classRoom: [
    {
      required: true,
      message: "请选择上课教室",
    },
  ],
  teacher: [
    {
      required: true,
      message: "请选择主班老师",
    },
  ],
  assistant: [
    {
      required: true,
      message: "请选择助教老师",
    },
  ],
};

Page({
  /**
   * 页面的初始数据
   */
  data: {
    query: {},
    className: "请选择班级",
    classId: "",
    classOptions: [], // 班级列表
    showClass: false,
    courseId: "",
    courseName: "请选择课程",
    courseOptions: [], // 课程列表
    showCourse: false,
    // 上课日期
    showStartDate: false,
    classDates: "请选择",
    currentStartDate: new Date().getTime(),
    minDate: new Date(2020, 0, 1).getTime(),
    // 上课时间
    showStartTime: false,
    classStartTime: "",
    filterStartTime(type, options) {
      if (type === "minute") {
        return options.filter((option) => option % 5 === 0);
      }
      return options;
    },
    // 下课时间
    classEndTime: "",
    showEndTime: false,
    filterEndTime(type, options) {
      if (type === "minute") {
        return options.filter((option) => option % 5 === 0);
      }
      return options;
    },
    // 上课教室
    classRoom: "",
    showRoom: false, // 上课教室
    classRoomName: "请选择",
    classRoomOptions: [],
    // 主班老师
    teacherName: "请选择",
    teacher: "",
    showTeacher: false, // 主班老师显示
    teacherOptions: [], // 主班老师列表
    // 助教老师
    showAssistant: false,
    assistantOptions: [], // 助教
    assistantName: "请选择", // 助教老师拼接显示
    assistant: "", // 助教老师拼接id
    description: "", // 上课内容
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    const param = JSON.parse(options.param);
    this.setData({
      query: param,
    });
    console.log("开班试听：", this.data.query);
    this.getClassList();
    // 获取教室
    await this.getClassRoomList();
    // 获取主班老师
    await this.getTeacherList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  // 获取班级
  getClassList() {
    classModel.getClassListApi({ pageNum: 1, pageSize: 9999 }).then((res) => {
      this.setData({
        classOptions: res.rows,
      });
    });
  },

  tapClass() {
    this.setData({
      showClass: true,
    });
  },
  chooseClassSelect(e) {
    const rows = e.detail;
    this.setData({
      className: rows[0].name,
      classId: rows[0].id,
      courseName: "请选择课程",
      courseId: "",
    });
    this.getClassCourseList(this.data.classId);
  },
  tapCourse() {
    if (this.data.classId) {
      this.setData({
        showCourse: true,
      });
    } else {
      Toast("请先选择班级！");
    }
  },
  // 根据班级获取课程
  getClassCourseList(id) {
    classModel.getClassCourseListApi({ id: id, open: "1" }).then((res) => {
      this.setData({
        courseOptions: res.courseList,
      });
    });
  },
  chooseCourseSelect(e) {
    const rows = e.detail;
    this.setData({
      courseName: rows[0].name,
      courseId: rows[0].id,
    });
  },

  // 开始日期
  showStartDatePopup() {
    this.setData({
      showStartDate: true,
    });
  },
  onCloseStartDate() {
    this.setData({
      showStartDate: false,
    });
  },
  chooseStartDate(e) {
    if (this.data.endDate != "请选择") {
      if (
        new Date(timestampToTime(e.detail)).getTime() >
        new Date(this.data.endDate).getTime()
      ) {
        Toast("开始日期不能大于结束日期");
        return;
      }
    }
    this.setData({
      classDates: timestampToTime(e.detail),
      currentStartDate: new Date(timestampToTime(e.detail)).getTime(),
      showStartDate: false,
    });
  },

  // 上课时间
  showStartTimePopup() {
    this.setData({
      showStartTime: true,
    });
  },
  // 选择上课时间
  chooseStartTime(e) {
    const date = new Date();
    const start_time = e.detail.split(":");
    const end_time = this.data.classEndTime.split(":");
    if (
      date.setHours(start_time[0], start_time[1]) >
      date.setHours(end_time[0], end_time[1])
    ) {
      Toast("上课时间不能大于下课时间");
      return;
    }
    this.setData({
      showStartTime: false,
      classStartTime: e.detail,
    });
  },
  onCloseStartTime() {
    this.setData({
      showStartTime: false,
    });
  },
  // 下课时间
  showEndTimePopup() {
    this.setData({
      showEndTime: true,
    });
  },
  // 选择下课时间
  chooseEndTime(e) {
    const date = new Date();
    const start_time = this.data.classStartTime.split(":");
    const end_time = e.detail.split(":");
    if (
      date.setHours(start_time[0], start_time[1]) >
      date.setHours(end_time[0], end_time[1])
    ) {
      Toast("下课时间不能小于上课时间");
      return;
    }
    this.setData({
      showEndTime: false,
      classEndTime: e.detail,
    });
  },
  onCloseEndTime() {
    this.setData({
      showEndTime: false,
    });
  },
  // 上课教室列表
  async getClassRoomList() {
    classModel.getClassRoomListApi({ lableType: "class_room" }).then((res) => {
      this.setData({
        classRoomOptions: res,
      });
    });
  },
  showRoomPopup() {
    this.setData({
      showRoom: true,
    });
  },
  chooseClassRoomSelect(e) {
    const rows = e.detail;
    this.setData({
      classRoomName: rows[0].name,
      classRoom: rows[0].id,
    });
  },
  // 获取主班老师列表
  async getTeacherList() {
    classModel.getTeacherListApi().then((res) => {
      let arr = [];
      if (res.length > 0) {
        res.forEach((item) => {
          const obj = {
            id: item.id,
            name: item.nickName,
          };
          arr.push(obj);
        });
      }
      this.setData({
        teacherOptions: arr,
        assistantOptions: arr,
      });
    });
  },
  // 主班老师
  showTeacherPopup() {
    this.setData({
      showTeacher: true,
    });
  },
  chooseTeacherSelect(e) {
    const rows = e.detail;
    this.setData({
      teacherName: rows[0].name,
      teacher: rows[0].id,
    });
  },
  showAssistantPopup() {
    this.setData({
      showAssistant: true,
    });
  },
  chooseAssistantSelect(e) {
    const rows = e.detail;
    let arrIds = [];
    let arrLabels = [];
    rows.forEach((item) => {
      arrIds.push(item.id);
      arrLabels.push(item.name);
    });
    this.setData({
      assistantName: arrLabels.join(","),
      assistant: arrIds.join(","),
    });
  },
  // 确定试听
  confirmListen() {
    const obj = {
      assistant: this.data.assistant,
      classDates: this.data.classDates,
      classEndTime: this.data.classEndTime,
      classId: this.data.classId,
      classRoom: this.data.classRoom,
      classStartTime: this.data.classStartTime,
      courseId: this.data.courseId,
      description: this.data.description,
      teacherId: this.data.teacher,
      stuIds: [this.data.query.id],
    };
    formRules(rules, this.data, (status) => {
      if (status) {
        classModel.startClassAuditionApi(obj).then((res) => {
          wx.navigateBack();
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
